/**
 * File Name   : MD5Util.java
 * Description : 
 */
package com.hh.common.util;

import java.security.MessageDigest;

import org.apache.log4j.Logger;

/**
 * @author 冯昌硕
 * @create 2012-12-17 下午9:16:00
 */
public class MD5Util {

    private static final Logger log = Logger.getLogger(MD5Util.class);
    
    /**
     * 生成MD5签名
     * 
     * @param strSource
     * @return
     * @create 2012-12-17 下午9:20:16
     */
    public static String encryptStringByMD5(String strSource) {
        
        if(strSource == null) {
            return null;
        }
        
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        
        try {
            byte[] strTemp = strSource.getBytes();
            //主要是这个类
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e){
            log.error("生成md5失败: "+e.toString());
            return null;
        }
    }
    
    public static void main(String[] args) {
        String md5Rs = encryptStringByMD5("admin");
        System.out.println("md5Rs=[" + md5Rs + "]");
    }
}
